package maa.action.admin.user;

import maa.action.Action;
import maa.action.ActionResult;
import maa.dao.DAOCommand;
import maa.dao.DAOFactory;
import maa.dao.DAOManager;
import maa.database.ConnectionPool;
import maa.entity.User;
import maa.action.ActionException;
import maa.dao.DAOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

public class UserUpdatePage implements Action {
    @Override
    public ActionResult execute(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        final HttpSession session = req.getSession();
        ActionResult actionResult = new ActionResult();
        ConnectionPool connectionPool = ConnectionPool.getInstance();
        DAOManager daoManager = DAOFactory.createDAOManager(connectionPool.getConnection());
        String string = req.getParameter("uuid").substring(0, req.getParameter("uuid").length() - 1);
        final UUID uuid = UUID.fromString(string);
        DAOCommand command = new DAOCommand() {
            @Override
            public Object execute(DAOManager daoManager) throws ActionException {
                User temp = null;
                try {
                    temp = daoManager.getUserDAO().findById(uuid);
                } catch (DAOException e) {
                    throw new ActionException("Exception in userDAO.findById(uuid)");
                }
                return temp;
            }
        };
        User user = (User) daoManager.executeAndClose(command);
        session.setAttribute("user1", user);
        actionResult.setResult("user-update");
        return actionResult;
    }
}
